package com.java3_8;

//懒汉单例模式
public class Demo2 {
    private String name;
    private int age;

    private Demo2(String name, int age) {
        this.name = name;
        this.age = age;
    }

    //线程不安全
    private static Demo2 demo2 = null;
//    private static synchronized Demo2 getDemo2(){
//        if(demo2 == null){
//            demo2 = new Demo2("zhangsan",20);
//        }
//        return demo2;
//    }

    public static Demo2 getDemo2(){
        if(demo2 == null){
            synchronized (Demo2.class) {
                demo2 = new Demo2("zhangsan",20);
            }
        }
        return demo2;
    }

    @Override
    public String toString() {
        return "Demo2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
